const express = require('express')
const updateUser = express.Router()
const db = require('../../mysql/db')

function query(data) {
    return new Promise((resolve, reject) => {
        db.query(`
        UPDATE user SET 
    account = ?,code = ?, mail = ?, name = ?, sex = ?, phone = ?, 
    address = ? WHERE account = ?`, [
            data.account,
            data.code,
            data.mail,
            data.name,
            data.sex,
            data.phone,
            data.address,
            data.account,
        ], (err, results) => {
            if (err) {
                reject(err);
                console.log("更新个人信息失败！");
            } else {
                console.log("更新个人信息成功！");
            }
        });
    });
}
updateUser.post('/updateUser', async (req, res) => {
    try {
        const dataRes = await query(req.body);
        res.send({
            code: 200,
            message: '请求成功！',
            result: dataRes,
        });
    } catch (err) {
        console.error(err);
        res.status(500).send({
            code: 500,
            message: '请求失败！',
            error: err,
        });
    }
});
module.exports = updateUser
